using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using System.IO;
namespace ProcessesSMS
{
	/// <summary>
	/// Summary description for VIETWEB.
	/// </summary>
	public class VIETWEB
	{
		#region Declare		
		private SMS_MO WMO;
		private SMS_MT WMT;		
		ArrayList arr = new ArrayList();
		public VIETWEB(SMS_MO MO)
		{
			WMO = MO;
		}

		public VIETWEB()
		{
		}
		#endregion
		#region Method
        public void run()
        {
            WMT = new SMS_MT(WMO);
            string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
            tmpMsg = tmpMsg.Replace("  ", " ").Trim();
            //string[] msg = tmpMsg.Split(new char[] { ' ' });
            string strReturnMsg = GetReturnMsg(tmpMsg);
            if (strReturnMsg != "")
            {
                WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
                WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
                WMT.Insert();
            }
            return;
        }
        public string GetReturnMsg(string arr)
        {
            string msgBody = string.Empty;            		
            string strcurrency = "500";
            if (WMO.ServiceID.Replace("04", "") == "6072")
                strcurrency = "500";
            else if (WMO.ServiceID.Replace("04", "") == "6172")
                strcurrency = "1000";
            else if (WMO.ServiceID.Replace("04", "") == "6272")
                strcurrency = "2000";
            else if (WMO.ServiceID.Replace("04", "") == "6372")
                strcurrency = "3000";
            else if (WMO.ServiceID.Replace("04", "") == "6472")
                strcurrency = "4000";
            else if (WMO.ServiceID.Replace("04", "") == "6572")
                strcurrency = "5000";
            else if (WMO.ServiceID.Replace("04", "") == "6672")
                strcurrency = "10000";
            else if (WMO.ServiceID.Replace("04", "") == "6772")
                strcurrency = "15000";

            TextWriter twriter = new StreamWriter("VWdir\\" + WMO.CommandCode.ToUpper() + "_" + WMO.MO_ID + ".txt");
            twriter.Write(arr + "," + WMO.UserID + "," + WMO.RequestTime + "," + strcurrency);
            twriter.Close();

            Insert_TLH_USERS(WMO.MO_ID,WMO.UserID, WMO.ServiceID, WMO.Message, WMO.RequestTime);

            msgBody = "Ban da nap " + strcurrency + " dong cho tai khoan " + arr + ".Cam on ban da su dung dich vu cua chung toi";           
            return msgBody;
        }
        private void Insert_TLH_USERS(int _MO_ID,string _USERID, string _SERVICEID, string _MESSAGE,DateTime _REQUESTTIME)
        {
            string strSQLInsert = "";
            string strSQLconnec = ConfigSettings.ReadSetting("CONNECTION_STRING_7x62");
            strSQLInsert = "INSERT INTO TLH(MO_ID,USERID, SERVICEID, MESSAGE, REQUESTTIME)";
            strSQLInsert += " VALUES(@MO_ID,@USERID, @SERVICEID, @MESSAGE, @REQUESTTIME)";
            SqlConnection cnn = new SqlConnection(strSQLconnec);
            cnn.Open();

            SqlCommand cmd;
            cmd = new SqlCommand(strSQLInsert, cnn);
            cmd.Parameters.Clear();

            cmd.Parameters.AddWithValue("@MO_ID",_MO_ID);
            cmd.Parameters.AddWithValue("@USERID", _USERID);
            cmd.Parameters.AddWithValue("@SERVICEID", _SERVICEID);
            cmd.Parameters.AddWithValue("@MESSAGE", _MESSAGE);
            cmd.Parameters.AddWithValue("@REQUESTTIME", _REQUESTTIME);

            cmd.ExecuteNonQuery();
            cmd.Dispose();
            cnn.Close();
        }
		#endregion
	}
}
